package com.insigma.business.common.service.impl;

import com.insigma.business.common.dto.CurrentUser;
import com.insigma.business.common.service.CurrentUserService;
import com.insigma.business.qrcode.common.dto.RpcContextAttachmentConstant;
import com.insigma.business.qrcode.common.util.BizCheckUtil;
import com.insigma.business.qrcode.common.util.BizException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Created by Administrator on 2019/1/15.
 */
@Service
public class CurrentUserServiceImpl implements CurrentUserService {
    @Autowired
    private HttpServletRequest request;

    @Value("${sso.mock}")
    private boolean isMock;

    @Value("${mock.openid}")
    private String openid;

    @Value("${mock.admdvs}")
    private String admdvs;

    @Value("${mock.userid}")
    private String userid;

    @Value("${mock.soldierId}")
    private String soldierId;

    @Value("${mock.logonName}")
    private String logonName;

    @Value("${mock.mobile}")
    private String mobile;


    @Override
    public CurrentUser getCurrentUser() {
        if (isMock) {
            return getMockInfo();
        }
        HttpSession session = request.getSession();
        CurrentUser userInfo = (CurrentUser) session.getAttribute(RpcContextAttachmentConstant.RPC_CONTEXT_KEY_SESSION_COMMON_USER);
        BizCheckUtil.checkNotNull(userInfo,"获取不到用户信息！");
        return userInfo;
    }

    private CurrentUser getMockInfo() {
        CurrentUser currentUser = new CurrentUser();
        currentUser.setWechatOpenid(openid);
        currentUser.setAdmdvs(admdvs);
        currentUser.setUserId(userid);
        currentUser.setSoldierId(soldierId);
        currentUser.setUserName(logonName);
        currentUser.setMobile(mobile);
        return currentUser;
    }

}
